package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.utils.JwtsUtils;
import com.bwie.utils.R;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.BrandMapper;
import com.bwie.pojo.Brand;
import com.bwie.service.BrandService;

import javax.servlet.http.HttpServletRequest;

/**
* @author :Beyond🎸🎸
* @date : 2025-10-17 19:59:27
* @description
* ${description}
**/
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService{


    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private HttpServletRequest request;




    @Override
    public R getBrandList(Brand brand) {

        Page<Brand> brandPage = new Page<>(brand.getPageNum(), brand.getPageSize());

        Page<Brand> page = brandMapper.getBrandList(brandPage, brand);

        return R.OK(page);
    }

    @Override
    public R saveBrand(Brand brand) {
        String token = request.getHeader("token");
        Integer userIdByToken = JwtsUtils.getUserIdByToken(token);
        brand.setBrandUse(1);
        brand.setCreateBy(userIdByToken);
        brand.setCreateTime(new Date());
        brand.setDeleteFlag(0);
        brandMapper.insert(brand);

        return R.OK();
    }

    @Override
    public R updateBrand(Brand brand) {

        Brand one = brandMapper.selectOne(new QueryWrapper<Brand>().lambda().like(Brand::getBrandId, brand.getBrandId()));
        if(one==null){
            R.ERROR(500,"该信息不存在");
        }
        String token = request.getHeader("token");
        Integer userIdByToken = JwtsUtils.getUserIdByToken(token);
        brand.setUpdateBy(userIdByToken);
        brand.setUpdateTime(new Date());
        brandMapper.updateById(brand);
        return R.OK();
    }

    @Override
    public R deleteBrandById(Brand brand) {

        Brand one = brandMapper.selectOne(new QueryWrapper<Brand>().lambda().like(Brand::getBrandId, brand.getBrandId()));

        if(one==null){
            R.ERROR(500,"该信息不存在");
        }

        String token = request.getHeader("token");
        Integer userIdByToken = JwtsUtils.getUserIdByToken(token);
        brand.setUpdateBy(userIdByToken);
        brand.setUpdateTime(new Date());
        brand.setDeleteTime(new Date());
        brand.setDeleteFlag(1);
        brandMapper.updateById(brand);

        return R.OK();
    }

    @Override
    public R deleteBatchByIds(List<Brand> ids) {

//        Brand brand = new Brand();
//        brand.setDeleteTime(new Date());
//        brand.setDeleteFlag(1);
//
//        LambdaUpdateWrapper<Brand> updateWrapper = new LambdaUpdateWrapper<>();
//        updateWrapper.in(Brand::getBrandId,ids);
//        brandMapper.update(brand,updateWrapper);
        for (Brand id : ids) {
            this.deleteBrandById(id);
        }
        return R.OK();


    }
}
